package com.loser.user.mongo.handler;

import com.loser.core.entity.Page;
import com.loser.core.wrapper.LambdaQueryWrapper;
import com.loser.core.wrapper.Wrappers;
import com.loser.module.base.common.APiResponsePage;
import com.loser.module.base.common.ApiResponse;
import com.loser.module.base.proto.sdk.user.request.UserLoginReq;
import com.loser.module.base.proto.sdk.user.resp.UserLoginResult;
import com.loser.rpc.RpcBalanceHandler;
import com.loser.rpc.RpcRoomHandler;
import com.loser.rpc.RpcSearchHandler;
import com.loser.rpc.room.dto.resp.RoomInfoResp;
import com.loser.rpc.sale.dto.resp.UserBalanceResp;
import com.loser.rpc.search.dto.resp.OptLogResp;
import com.loser.rpc.user.dto.req.UserPageReq;
import com.loser.rpc.user.dto.req.UserSaveReq;
import com.loser.rpc.user.dto.req.UserUpdateReq;
import com.loser.rpc.user.dto.resp.UserInfoResp;
import com.loser.user.mongo.entity.User;
import com.loser.user.mongo.service.UserService;
import com.loser.utils.ParamsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 测试数据 处理器
 *
 * @author loser
 * @date 2023-02-05  14:01
 */
@Slf4j
@Component
public class UserHandler {

    @Autowired
    private UserService userService;
    @Autowired
    private RpcRoomHandler rpcRoomHandler;
    @Autowired
    private RpcBalanceHandler rpcBalanceHandler;
    @Autowired
    private RpcSearchHandler rpcSearchHandler;

    /**
     * 新增测试数据
     */
    public ApiResponse<Boolean> save(UserSaveReq req) {

        ParamsUtil.checkNotNull(req);
        User save = ParamsUtil.copyProperties(req, User.class);
        return ApiResponse.ok(userService.save(save));

    }

    /**
     * 修改测试数据
     */
    public ApiResponse<Boolean> update(UserUpdateReq req) {

        ParamsUtil.checkNotNull(req);
        User update = ParamsUtil.copyProperties(req, User.class);
        return ApiResponse.ok(userService.updateById(update));

    }

    /**
     * 通过id删除测试数据
     */
    public ApiResponse<Boolean> deleteById(Long id) {

        ParamsUtil.checkNotNull(id);
        return ApiResponse.ok(userService.removeById(id));

    }

    /**
     * 通过id获取测试数据
     */
    public ApiResponse<UserInfoResp> getById(Long id) {

        ParamsUtil.checkNotNull(id);
        RoomInfoResp roomInfo = rpcRoomHandler.getById(id).getData();
        UserBalanceResp balance = rpcBalanceHandler.getByUserId(id).getData();
        User dbData = userService.getById(id);
        UserInfoResp resp = ParamsUtil.copyProperties(dbData, UserInfoResp.class);
//        OptLogReq log = new OptLogReq();
//        log.setUserId(id);
//        log.setTitle("查询用户信息");
//        log.setContent("查询用户信息-" + DateUtils.now());
//        rpcSearchHandler.save(log);
        List<OptLogResp> logs = rpcSearchHandler.getList(id).getData();
        resp.setUserBalanceResp(balance);
        resp.setRoomInfo(roomInfo);
        resp.setLogs(logs);
        log.info("user get {}", resp);
        return ApiResponse.ok(resp);

    }

    /**
     * 分页获取测试数据
     */
    public APiResponsePage<UserInfoResp, Object> queryList(UserPageReq req) {

        LambdaQueryWrapper<User> query = Wrappers.<User>lambdaQuery()
                .eq(Objects.nonNull(req.getAge()), User::getAge, req.getAge())
                .eq(Objects.nonNull(req.getLoginName()), User::getLoginName, req.getLoginName())
                .eq(Objects.nonNull(req.getPassWord()), User::getPassWord, req.getPassWord())
                .eq(Objects.nonNull(req.getAge()), User::getAge, req.getAge())
                .between((Objects.nonNull(req.getStartTime()) && Objects.nonNull(req.getEndTime())), User::getCreateTime, req.getStartTime(), req.getEndTime());
        Page<User> page = userService.page(query, req.getPageNo(), req.getPageSize());
        return APiResponsePage.okPage(page.getPageSize(), page.getPageSize(), page.getTotal(), ParamsUtil.mapList(page.getRecords(), UserInfoResp.class));

    }

    public UserLoginResult login(UserLoginReq req) {

//        User dbData = userService.getById(req.getUserId());
//        AssertUtil.isTrue(Objects.isNull(dbData), "用户信息不存在");
//        AssertUtil.notTrue(dbData.getPassWord().equals(req.getPassWord()), "密码错误");
        UserLoginResult result = new UserLoginResult();
        result.setUserName("test" + req.getUserId());
        result.setUserId(req.getUserId());
        return result;

    }

}
